package com.baymax.test.scene.demo

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.drawable.DrawableUtils
import com.baymax.test.scene.newSceneFlexContainer
import com.facebook.drawee.drawable.ForwardingDrawable
import com.facebook.drawee.drawable.MatrixDrawable
import com.google.android.flexbox.FlexboxLayout
import kotlinx.coroutines.channels.Channel
import kotlin.properties.Delegates

class TextDrawableScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        val ctx = p1.context
        val m = Matrix()
        var hor  by Delegates.observable(10f ) { p, o, n ->
            m.postTranslate(0f , n - o)
        }
        return p1.newSceneFlexContainer {
            val t1 = TextView(ctx)
            t1.text = "this is text"
            addView(t1 , FlexboxLayout.LayoutParams(-2 , 100))
            val d = DrawableUtils.createGradientDrawable(Color.RED, 100f)



            t1.setCompoundDrawablesRelative(null ,null ,null ,object :ForwardingDrawable(d){
                init {
                    setBounds(0 ,0 ,100, 10)
                    m.setTranslate( 0f, -20f)
                }
                val p = Paint(Paint.ANTI_ALIAS_FLAG).apply {
                    color = Color.GREEN
                    style = Paint.Style.FILL
                    strokeWidth = 4f
                }
                override fun draw(canvas: Canvas) {
                    canvas.translate(0f , hor)
                    canvas.drawLine(10f , hor , 90f , hor +10f , p)
                    canvas.drawCircle(10f,hor ,5f , p)
                    super.draw(canvas)

                }
            })

            val t2 = TextView(ctx)
            t2.text = "this is text2 "
            addView(t2 ,FlexboxLayout.LayoutParams(-2 , 100))

            addButton("+"){
                hor += 2f
              t1. requestLayout()
                t1.postInvalidate()
            }
            addButton("-") {
                hor -= 2f
               t1. requestLayout()
                t1.postInvalidate()
            }

        }
    }
}